package com.xinqi.modules.exam.paper.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.exam.paper.convert.CourseExamPaperConfigConvert;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperConfigEntity;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperConfigCreateDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperConfigDeleteDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperConfigQueryDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperConfigUpdateDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperConfigResultDTO;
import com.xinqi.modules.exam.paper.manager.CourseExamPaperConfigManager;
import com.xinqi.modules.exam.paper.service.CourseExamPaperConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 课程测评配置服务层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Validated
@Service("courseExamPaperConfigService")
@RequiredArgsConstructor
public class CourseExamPaperConfigServiceImpl implements CourseExamPaperConfigService {
    private final CourseExamPaperConfigConvert courseExamPaperConfigConvert;
    private final CourseExamPaperConfigManager courseExamPaperConfigManager;

    /**
     * 分页查询 课程测评配置数据
     */
    @Override
    @DS("slaver")
    public PageInfo<CourseExamPaperConfigResultDTO> page(Param pageable, CourseExamPaperConfigQueryDTO query) {
        IPage<CourseExamPaperConfigEntity> page = courseExamPaperConfigManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, courseExamPaperConfigConvert::convert);
    }

    /**
     * 查询 课程测评配置数据
     */
    @Override
    public List<CourseExamPaperConfigResultDTO> find(CourseExamPaperConfigQueryDTO query) {
        return courseExamPaperConfigManager.list(wrapper(query)).stream()
            .map(courseExamPaperConfigConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<CourseExamPaperConfigEntity> wrapper(CourseExamPaperConfigQueryDTO query) {
        LambdaQueryWrapper<CourseExamPaperConfigEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(CourseExamPaperConfigEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 课程测评配置数据
     */
    @Override
    public CourseExamPaperConfigResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(courseExamPaperConfigManager.findById(id)).map(courseExamPaperConfigConvert::convert).orElse(null);
    }

    @Override
    public CourseExamPaperConfigResultDTO findByExamId(Long examId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(examId), "测评ID不能为空");
        return Optional.ofNullable(courseExamPaperConfigManager.findByExamId(examId)).map(courseExamPaperConfigConvert::convert).orElse(null);
    }

    /**
     * 新增 课程测评配置数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid CourseExamPaperConfigCreateDTO dto) {
        CourseExamPaperConfigEntity entity = courseExamPaperConfigConvert.create(dto);
        courseExamPaperConfigManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 课程测评配置数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid CourseExamPaperConfigUpdateDTO dto) {
        Optional.ofNullable(courseExamPaperConfigManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        CourseExamPaperConfigEntity entity = courseExamPaperConfigConvert.update(dto);
        return courseExamPaperConfigManager.updateById(entity);
    }

    /**
     * 根据id删除 课程测评配置数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return courseExamPaperConfigManager.removeById(id);
    }

    /**
     * 根据id删除 课程测评配置数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(CourseExamPaperConfigDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
